#include "graph_node.pb.h"
#include <google/protobuf/util/json_util.h> 
#include <iostream>
#include <fstream>

class GraphSingleton {
public:
    // 禁止实例化
    GraphSingleton() = delete;

    // 静态方法访问 Graph
    static Graph& getGraph() {
        static Graph instance;  // 保证只创建一次
        return instance;
    }
    // 静态方法添加节点
    static void addNode(const Node& node) {
        getGraph().add_nodes()->CopyFrom(node);
    }
    static void addNode(Crypto cryptoType) {
    // 创建新节点
        Node* node = getGraph().add_nodes();
        node->set_cryptotype(cryptoType);
    }
    // 其他静态方法...
    static void dump() {
        std::string json_output;
        google::protobuf::util::MessageToJsonString(GraphSingleton::getGraph(), &json_output);
        std::cout << "Serialized to JSON string: " << json_output << std::endl;

        // std::fstream output("node.bin", std::ios::out | std::ios::trunc | std::ios::binary);
        // if (!GraphSingleton::getGraph().SerializeToOstream(&output)) {
        //     std::cerr << "Failed to write node." << std::endl;
        //     return -1;
        // }
    }
    static void dump(const std::string& filePath) {
        std::string json_output;
        google::protobuf::util::MessageToJsonString(GraphSingleton::getGraph(), &json_output);

        std::ofstream output(filePath, std::ios::out | std::ios::trunc);
        if (!output.is_open()) {
            std::cerr << "Failed to open file: " << filePath << std::endl;
            return;
        }

        output << json_output;
        if (!output.good()) {
            std::cerr << "Failed to write to file: " << filePath << std::endl;
        } else {
            std::cout << "Graph JSON successfully written to file: " << filePath << std::endl;
        }
    }
};
